package domain.entities;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * InvestigadorProtocoloCentro entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "investigador_protocolo_centro", catalog = "bms")
public class InvestigadorProtocoloCentro implements java.io.Serializable {

	// Fields

	private InvestigadorProtocoloCentroId id;
    private ProtocoloCentro protocoloCentro;
	private Involucrado principalInvestigator;

    private String piPosition;
    private String piLocation;
    private String piPhoneNumber;

    private Involucrado subInvestigator;
    private boolean subRequiresSafety;
    private String subPhoneNumber;

	private SiteState status;//"postuladed" por defecto// poner enums
    private String justification;
    private String siteScoreComment;
    private Score piScore;
    private String piScoreComment;


    private List<RoleNameProtocoloCompromisoPais> siteStaff = new ArrayList<RoleNameProtocoloCompromisoPais>();

    // Constructors

	/** default constructor */
	public InvestigadorProtocoloCentro() {
	}

	/** minimal constructor */
	public InvestigadorProtocoloCentro(InvestigadorProtocoloCentroId id, ProtocoloCentro protocoloCentro,
			 String status) {
		this.id = id;
		this.protocoloCentro = protocoloCentro;
	}

	/** full constructor */
	public InvestigadorProtocoloCentro(InvestigadorProtocoloCentroId id, ProtocoloCentro protocoloCentro
			) {
		this.id = id;
		this.protocoloCentro = protocoloCentro;
	}

	// Property accessors
	@EmbeddedId
	@AttributeOverrides( {
			@AttributeOverride(name = "investigadorid", column = @Column(name = "INVESTIGADORid", nullable = false)),
			@AttributeOverride(name = "protocoloCentrocentroid", column = @Column(name = "PROTOCOLO_CENTROCENTROid", nullable = false)),
			@AttributeOverride(name = "protocoloCentroprotocoloCompromisopaisprotocoloid", column = @Column(name = "protocolo_centroprotocolo_compromisopaisprotocoloid", nullable = false)),
			@AttributeOverride(name = "protocoloCentroprotocoloCompromisopaiscompromisopaisid", column = @Column(name = "protocolo_centroprotocolo_compromisopaiscompromisopaisid", nullable = false)) })
	public InvestigadorProtocoloCentroId getId() {
		return this.id;
    }

	public void setId(InvestigadorProtocoloCentroId id) {
		this.id = id;
	}

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinColumns( {
        @JoinColumn(name="investigadorid_fk", referencedColumnName="INVESTIGADORid"),
        @JoinColumn(name="protocoloCentrocentroid_fk", referencedColumnName="PROTOCOLO_CENTROCENTROid"),
        @JoinColumn(name="protocoloCentroprotocoloCompromisopaisprotocoloid_fk", referencedColumnName="protocolo_centroprotocolo_compromisopaisprotocoloid"),
        @JoinColumn(name="protocoloCentroprotocoloCompromisopaiscompromisopaisid_fk", referencedColumnName="protocolo_centroprotocolo_compromisopaiscompromisopaisid")
    })
    public List<RoleNameProtocoloCompromisoPais> getSiteStaff() {
        return siteStaff;
    }

    public void setSiteStaff(List<RoleNameProtocoloCompromisoPais> siteStaff) {
        this.siteStaff = siteStaff;
    }

    @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH}, fetch = FetchType.EAGER)
	@JoinColumns( {
			@JoinColumn(name = "PROTOCOLO_CENTROCENTROid", referencedColumnName = "CENTROid", nullable = false, insertable = false, updatable = false),
			@JoinColumn(name = "protocolo_centroprotocolo_compromisopaisprotocoloid", referencedColumnName = "protocolo_compromisopaisprotocoloid", nullable = false, insertable = false, updatable = false),
			@JoinColumn(name = "protocolo_centroprotocolo_compromisopaiscompromisopaisid", referencedColumnName = "protocolo_compromisopaiscompromisopaisid", nullable = false, insertable = false, updatable = false) })
	public ProtocoloCentro getProtocoloCentro() {
		return this.protocoloCentro;
	}

	public void setProtocoloCentro(ProtocoloCentro protocoloCentro) {
		this.protocoloCentro = protocoloCentro;
	}

	@ManyToOne
	@JoinColumn(name = "PIid", nullable = true, insertable = true, updatable = true)
	public Involucrado getPrincipalInvestigator() {
		return this.principalInvestigator;
	}

	public void setPrincipalInvestigator(Involucrado principalInvestigator) {
		this.principalInvestigator = principalInvestigator;
	}

    public String getPiPosition() {
        return piPosition;
    }

    public void setPiPosition(String piPosition) {
        this.piPosition = piPosition;
    }

    @ManyToOne
    @JoinColumn(name = "SIid", nullable = true, insertable = true, updatable = true)
    public Involucrado getSubInvestigator() {
        return subInvestigator;
    }

    public void setSubInvestigator(Involucrado subInvestigator) {
        this.subInvestigator = subInvestigator;
    }

    @Column(name="status")
    @Enumerated(EnumType.STRING)
    public SiteState getStatus() {
        return status;
    }

    public void setStatus(SiteState status) {
        this.status = status;
    }

    public String getJustification() {
        return justification;
    }

    public void setJustification(String justification) {
        this.justification = justification;
    }

    public String getSiteScoreComment() {
        return siteScoreComment;
    }

    public void setSiteScoreComment(String siteScoreComment) {
        this.siteScoreComment = siteScoreComment;
    }

    @Column(name="piScore")
    @Enumerated(EnumType.ORDINAL)
    public Score getPiScore() {
        return piScore;
    }

    public void setPiScore(Score piScore) {
        this.piScore = piScore;
    }

    public String getPiScoreComment() {
        return piScoreComment;
    }

    public void setPiScoreComment(String piScoreComment) {
        this.piScoreComment = piScoreComment;
    }

    public String getPiLocation() {
        return piLocation;
    }

    public void setPiLocation(String piLocation) {
        this.piLocation = piLocation;
    }

    public String getPiPhoneNumber() {
        return piPhoneNumber;
    }

    public void setPiPhoneNumber(String piPhoneNumber) {
        this.piPhoneNumber = piPhoneNumber;
    }

    public String getSubPhoneNumber() {
        return subPhoneNumber;
    }

    public void setSubPhoneNumber(String subPhoneNumber) {
        this.subPhoneNumber = subPhoneNumber;
    }
    public boolean isSubRequiresSafety() {
        return subRequiresSafety;
    }

    public void setSubRequiresSafety(boolean subRequiresSafety) {
        this.subRequiresSafety = subRequiresSafety;
    }

    @Transient
    public boolean isNew(){
        return id ==null ||
                id.getInvestigadorid() == null ||
                id.getProtocoloCentrocentroid() == null ||
                id.getProtocoloCentroprotocoloCompromisopaiscompromisopaisid() == null ||
                id.getProtocoloCentroprotocoloCompromisopaisprotocoloid() == null;
    }

    private Ongoing ongoing;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "investigadorProtocoloCentro")
    public Ongoing getOngoing() {
        return ongoing;
    }

    public void setOngoing(Ongoing ongoing) {
        this.ongoing = ongoing;
    }

    @Override
    public String toString(){
        return getProtocoloCentro().getCentro().getInstitucion()+" - "+
                getPrincipalInvestigator().getNombre()+" "+getPrincipalInvestigator().getApellido();
    }


}